﻿using System;
using System.Windows.Forms;

namespace TimerTaskTools
{
    public partial class Form1 : Form
    {

        static Timer _timer;

        static Timer tastk_timer;

        public Form1()
        {
            InitializeComponent();
            SetCurrTime();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void SetCurrTime() {

            _timer = new Timer { Interval = 1000 }; // 1秒间隔
            _timer.Tick += (sender, e) =>
            {
                label5.Text = DateTime.Now.ToString("yyyy-MM_dd HH:ss"); // 直接更新UI
            };
            _timer.Start();
        }
        private void StartTask(int interval)
        {
            textBox2.Text += "------------------------------定时任务开始-------------------------------------------\r\n";
            tastk_timer = new Timer { Interval = interval*1000 };

            tastk_timer.Tick +=  (sender, e) =>
            {
                textBox2.Text += "当前时间：" + DateTime.Now.ToString("yyyy-MM_dd HH:ss") + " \r\n";  // 直接更新UI
            };

            /* tastk_timer.Tick +=async (sender, e) =>
            {
                textBox2.Text += "当前时间："+ DateTime.Now.ToString("yyyy-MM_dd HH:ss") + " \r\n";  // 直接更新UI

                string res= await GrayHelpUtils.GetGrayList();
                textBox2.Text += "查询内容"+res+ "\r\n"; // 直接更新UI
            };*/
            tastk_timer.Start();
        }
        private void StopTask() {

            textBox2.Text += "------------------------------定时任务结束-------------------------------------------\r\n";
            tastk_timer.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int.TryParse(this.textBox3.Text, out int interval);
            if (interval<=0)
            {
                MessageBox.Show("请输入正确的间隔时间");
                return;
            }
            StartTask(interval);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StopTask();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }
    }
}
